/**
 *  The below class does not use Sharing keyword as one of our customer had a requirement where in Standard Role Hierarchy is not followed.
 */
public with sharing class SearchPlanDocumentsForRmVF{
  
    String status = '';
    String roles = '';
    List<Sales_Contract__c> contracts = null; 
      
    String userProfileName = '';
    Profile[] p = null;
    private Date effStartDate = null;
    private Date effEndDate = null;
    Map<ID, User> uRoleMap = new Map<ID, User>();
    Map<ID, User> tempRoleMap = new Map<ID, User>();
    Map<ID, User> temp2RoleMap = new Map<ID, User>();
    private Map<ID, ID[]> usersInRoleMap = new Map<ID, ID[]>();
    private Map<ID, ID> userMap1 = new Map<ID, ID>();
    private Map<ID, ID> userMap2 = new Map<ID, ID>();
    private Map<ID, ID> userMap3 = new Map<ID, ID>();
    private String[] usersListFromMap = null;
    private String userId = UserInfo.getUserId();

    static String testVar = '';
    private String fieldName = null; 
    public Boolean displayFlag {get; set;}
    public Boolean isAdmin {get; set;}
    public Boolean isEndUser {get; set;}
    public Plan_Document_Inquiry__c pdi {get; set;}
    public Sales_Contract__c planDocument  {get; set;}
    public Boolean isNonMergeSearch {get; set;}    
    public Boolean isMergeSearch {get; set;}
    public Boolean isRoleEnabled {get; set;}   
    private String errorMessage = '';
    
    public SearchPlanDocumentsForRmVF()
    {
        Boolean hasPlanDocEDPermission;
        planDocument = new Sales_Contract__c();
        pdi = new Plan_Document_Inquiry__c();
        
        status = 'Published';
        try
        {
            Reporting_Manager__c[] rmList = [select Reporting_Manager__c from Reporting_Manager__c limit 1];

		    if(rmList.size() > 0)
		    	fieldName = rmList[0].Reporting_Manager__c;
	    	else
	    		fieldName = 'ManagerId';

	    	if(fieldName == null)
	    		fieldName = 'ManagerId';
            
            isRoleEnabled = isAdmin = isEndUser = displayFlag = isMergeSearch = isNonMergeSearch = false;
            URL__c[] urlList = [select Plan_Doc_Type__c from URL__c limit 1];
            if(urlList.size() > 0 && urlList[0].Plan_Doc_Type__c == 'no')
            {
                isNonMergeSearch = !isNonMergeSearch;
            }//end of if(urlList.size() > 0 && urlList[0].Plan_Doc_Type__c == 'no')
            else
            {
                isMergeSearch = !isMergeSearch;
            }
            
            Remote_IP_Address__c[] ripList = [select Is_Role_Shown__c from Remote_IP_Address__c limit 1];
            if(ripList.size() > 0)
            	isRoleEnabled = ripList[0].Is_Role_Shown__c;
            
            Schema.DescribeSObjectResult pdPermission = Sales_Contract__c.SObjectType.getDescribe();
            if(pdPermission.isUpdateable() == true && pdPermission.isDeletable() == true && pdPermission.isCreateable() == true)
                hasPlanDocEDPermission = true;
            else
                hasPlanDocEDPermission = false;

            if(hasPlanDocEDPermission)
                isAdmin = !isAdmin;
            else
                isEndUser = !isEndUser;
            
                
            getDisplayColumns();
            
            User[] us = [select Id, ProfileId, Title, UserRole.Id, UserRole.Name from User u where u.Id = :UserInfo.getUserId()];
            if(us.size() > 0)
            {
               // p = [select Id, Name from Profile p where p.Id = :us[0].ProfileId];
               // if(p.size() > 0)
                //{
                  //  setUserProfileName(p[0].Name);
                    if(isAdmin == true && ApexPages.currentPage().getParameters().get('ur') == null)
                        planDocument.User_ID__c = UserInfo.getUserId();
                    else if(isAdmin == true && ApexPages.currentPage().getParameters().get('ur') != '')
                        planDocument.User_ID__c = ApexPages.currentPage().getParameters().get('ur');
                    
                    if(isEndUser == true && ApexPages.currentPage().getParameters().get('ur') == null)
                        pdi.User__c = UserInfo.getUserId();
                    else if(isEndUser == true && ApexPages.currentPage().getParameters().get('ur') != '')
                        pdi.User__c = ApexPages.currentPage().getParameters().get('ur');
    
                    if(ApexPages.currentPage().getParameters().get('pg') != null && ApexPages.currentPage().getParameters().get('pg') != '')                
                        planDocument.Batch__c = ApexPages.currentPage().getParameters().get('pg');
                    
                    if(ApexPages.currentPage().getParameters().get('ss') != null && ApexPages.currentPage().getParameters().get('ss') != '')
                        status = ApexPages.currentPage().getParameters().get('ss');
                    
                    if(ApexPages.currentPage().getParameters().get('tp') != null && ApexPages.currentPage().getParameters().get('tp') != '')
                        planDocument.Template__c = ApexPages.currentPage().getParameters().get('tp');
                    
                    if(ApexPages.currentPage().getParameters().get('rl') != null && ApexPages.currentPage().getParameters().get('rl') != '')
                        roles = ApexPages.currentPage().getParameters().get('rl');
                    
               // }

                uRoleMap.put(UserInfo.getUserRoleId(), us[0]);
            }
            
            if(!isAdmin)
            {
                usersInRoleMap.put(UserInfo.getUserRoleId(), new String[]{UserInfo.getUserId()});
                for(User[] uList : Database.query('Select id, name, UserRole.Id, UserRole.Name from User Where '+fieldName+' = :userId order by name'))
                    for(User u: uList)
                    {
                        tempRoleMap.put(u.Id, u);
                        uRoleMap.put(u.UserRole.Id, u);
                        usersListFromMap = usersInRoleMap.get(u.UserRole.Id);
                        if(usersListFromMap != null)
                        {
                            usersListFromMap.add(u.id);
                            usersInRoleMap.put(u.UserRole.Id, usersListFromMap);
                        }
                        else
                        {
                          usersListFromMap = new String[]{};
                          usersListFromMap.add(u.Id);
                          usersInRoleMap.put(u.UserRole.Id, usersListFromMap);
                        }
        
                    }
        
                while(tempRoleMap.size() > 0 && Limits.getQueries() < 94) 
                {
                    temp2RoleMap = tempRoleMap.deepClone();
                    tempRoleMap.clear();
                    tempRoleMap = getRolesAtDeeperLevel(temp2RoleMap);
                }
                
            }
            
            if(!isRoleEnabled)
            {
            	roles = UserInfo.getUserRoleId();
            	//getRolesList();
            }
			 
        }
        catch(Exception e)
        {
            errorMessage = e.getMessage();
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
        }
    }
    
    private Map<ID, User> getRolesAtDeeperLevel(Map<ID, User> temp2RoleMap)
    {
        Set<ID> userIdSet = temp2RoleMap.keySet();
        for(User[] uList : Database.query('Select id, name, UserRole.Id, UserRole.Name from User Where '+fieldName+' in :userIdSet order by name'))
            for(User u: uList)
            {
                tempRoleMap.put(u.Id, u);
                uRoleMap.put(u.UserRole.Id, u);
                usersListFromMap = usersInRoleMap.get(u.UserRole.Id);
                if(usersListFromMap != null)
                {
                    usersListFromMap.add(u.id);
                    usersInRoleMap.put(u.UserRole.Id, usersListFromMap);
                }
                else
                {
                  usersListFromMap = new String[]{};
                  usersListFromMap.add(u.Id);
                  usersInRoleMap.put(u.UserRole.Id, usersListFromMap);
                }
    
            }

         return tempRoleMap;
    }
    
    //Used to display columns Template, Plan Group Name if User is an Admin
    //Hide the columns if User is an End User
    public void getDisplayColumns()
    {
        Schema.DescribeSObjectResult tPermission = Template__c.SObjectType.getDescribe();
        if(tPermission.isCreateable() && tPermission.isDeletable() == true && tPermission.isCreateable() == true)
            displayFlag = true;
    }
    
    
    public void setUserProfileName(String name)
    {
        userProfileName = name;
    }
    
    public String getUserProfileName()
    {
        return userProfileName;
    }
    
    public List<SelectOption> getRolesList() {
        List<SelectOption> options = new List<SelectOption>();
        //options.add(new SelectOption('select','Select'));
        if(!isAdmin)
        {
            if(uRoleMap.size() > 0)
            {
                List<User> userRoleList = uRoleMap.values();
                for(User us: userRoleList)
                {
                    options.add(new SelectOption(us.UserRole.Id,us.UserRole.Name));
                    if(options.size() == 1000)
                        break;
                }
            }
        }
        else
        {
            for(UserRole[] userRole : [select Id, Name from UserRole order by Name limit 1000])
            {
                for(UserRole ur : userRole)
                {
                    options.add(new SelectOption(ur.Id,ur.Name));
                    
                    if(options.size() == 1000)
                        break;
                }
                if(options.size() == 1000)
                    break;
            }
        }
        return options;
    }
  
    public String getRoles() 
    {
        roles = UserInfo.getUserRoleId();
        return roles;
    }
  
    public void setRoles(String roles) {
        this.roles = roles;
    }

    public List<SelectOption> getStatusList() 
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('select','Select'));
        //Display the below two status only if User is Admin.
        if(displayFlag == true || isAdmin == true)
        {
            options.add(new SelectOption('Draft','Draft'));
	        options.add(new SelectOption('Distributed','Distributed'));
        }
        options.add(new SelectOption('Published','Published'));
        options.add(new SelectOption('Viewed','Viewed'));
        options.add(new SelectOption('Accepted','Accepted'));
        options.add(new SelectOption('Rejected','Rejected'));
//        options.add(new SelectOption('Plan Approved%','Manager Approved'));
  //      options.add(new SelectOption('Plan Rejected%','Manager Rejected'));
        options.add(new SelectOption('Inquiry Submitted','Inquiry Submitted'));
        options.add(new SelectOption('Inquiry Approved','Inquiry Approved'));
        options.add(new SelectOption('Inquiry Rejected','Inquiry Rejected'));

        return options;
    }//end of getStatusList

    public String getStatus()
    {
        if(status == null)
        {
             status = 'Published';
        }

        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
  
    public Plan_Document_Inquiry__c getStartDate()
    {
        return pdi;
    }    

    public List<Sales_Contract__c> getContracts()
    {
        return contracts;
    }
    
    public void setContracts(List<Sales_Contract__c> contracts) {
        this.contracts = contracts;
    }
    
    public PageReference doSearch()
    {
        if(errorMessage.length() > 0)
        {
            if(errorMessage.indexOf('No such column') != -1)
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'There is no such column '+fieldName+' existing in the User object.'));
            errorMessage = '';   
            return null;        
        }
        
        String percentile = '%';
        ID userId = null;
        try
        {
            if(isAdmin)
                userId = planDocument.User_ID__c;
            else
                userId = pdi.User__c;
            
            String templateName = '%';
            ID batchId = planDocument.Batch__c;  
            Date startDate = pdi.Effective_Start_Date__c;
            Date endDate = pdi.Effective_End_Date__c;
            if(isAdmin)        
                ApexPages.currentPage().getParameters().put('ur', planDocument.User_ID__c);
            if(isEndUser)
                ApexPages.currentPage().getParameters().put('ur', pdi.User__c);
            
            ApexPages.currentPage().getParameters().put('rl', roles);
            ApexPages.currentPage().getParameters().put('pg', planDocument.Batch__c);
            ApexPages.currentPage().getParameters().put('tp', planDocument.Template__c);
            ApexPages.currentPage().getParameters().put('ss', status);
            
            String[] statusListEndUser = new String[]{'Viewed', 'Published', 'Accepted', 'Rejected', 'Inquiry Submitted', 'Inquiry Approved', 'Inquiry Rejected'};
    
            if(contracts != null)
                contracts.clear();
    
            Template__c[] templateList = [select name from template__c where id = :planDocument.Template__c];
            if(templateList.size() > 0)
                templateName = templateList[0].name;
            
            String formWhereClause = '';
            
            if(status != 'select')
                formWhereClause += ' Status__c = :status ';
            else if(displayFlag)
                formWhereClause += ' Status__c like :percentile ';
            else
                formWhereClause += ' Status__c in :statusListEndUser ';
            
            
            if(isAdmin == true && planDocument.User_ID__c != null && isRoleEnabled)
                formWhereClause += ' and User_ID__c = :userId ';
            else if(isEndUser == true && pdi.User__c != null && isRoleEnabled)
                formWhereClause += ' and User_ID__c = :userId ';

            if(isEndUser == true && roles != 'select' && isRoleEnabled)
            {
                usersListFromMap = usersInRoleMap.get(roles);
				if(usersListFromMap != null)
				{                
	                if(userId  != null)
	                {
	                    Boolean flag = false;
	                    for(String userObjId : usersListFromMap)
	                    {
	                        if(userObjId == userId)
	                        {
	                            flag = true;
	                        }   
	                    }
	                    if(!flag)
	                    {
	                        contracts = null;
	                        return null;
	                    }
	                }
				}
                
                formWhereClause += ' and User_ID__c in :usersListFromMap ';
            }
        
        	if(isEndUser == true && !isRoleEnabled && pdi.User__c != null)
        	{
        		User[] selectedUser = [select Userrole.id from User where id = :userId];
        		if(selectedUser.size() > 0)
        		{
	        		roles = selectedUser[0].Userrole.id;
	                usersListFromMap = usersInRoleMap.get(roles);
                    Boolean flag = false;
	                if(userId  != null)
	                {
	                    if(usersListFromMap != null)
	                    {
		                    for(String userObjId : usersListFromMap)
		                    {
		                        if(userObjId == userId)
		                        {
		                            flag = true;
		                        }   
		                    }
		                    if(!flag)
		                    {
		                        contracts = null;
		                        return null;
		                    }
	                    }
	                }
	                if(flag == true)
		                formWhereClause += ' and User_ID__c = :userId ';
		            else 
		                formWhereClause += ' and User_ID__c = null ';
            	}
        	}
        
    		String[] usersList = new String[]{};
        	if(isEndUser == true && !isRoleEnabled && pdi.User__c == null)
        	{
        		for(ID roleIdVal: usersInRoleMap.keyset())
        		{
        			ID[] userIdList = usersInRoleMap.get(roleIdVal);
        			if(userIdList != null)
        			{
	    				for(ID uID: userIdList)
	    				{
	    					if(usersList.size() <1000)
	    						usersList.add(uId);
	    				}
        			}
                }

                if(usersList != null && usersList.size() > 0)
                	formWhereClause += ' and User_ID__c in :usersList ';
        		
/*        		User[] selectedUser = [select Userrole.id from User where id = :Userinfo.getUserid()];
        		if(selectedUser.size() > 0)
        		{
	        		roles = selectedUser[0].Userrole.id;
	                usersListFromMap = usersInRoleMap.get(roles);
	        		if(usersListFromMap != null)                
	                	formWhereClause += ' and User_ID__c in :usersListFromMap ';
	                else
		                formWhereClause += ' and User_ID__c = null ';
        		}  */
        	}

        	if(!isRoleEnabled && isAdmin && planDocument.User_ID__c == null)
        	{
        		for(ID roleIdVal: usersInRoleMap.keyset())
        		{
        			ID[] userIdList = usersInRoleMap.get(roleIdVal);
        			if(userIdList != null)
        			{
	    				for(ID uID: userIdList)
	    				{
	    					if(usersList.size() <1000)
	    						usersList.add(uId);
	    				}
        			}
                }

                if(usersList != null && usersList.size() > 0)
                	formWhereClause += ' and User_ID__c in :usersList ';
        	}
        	

        	if(!isRoleEnabled && isAdmin && planDocument.User_ID__c != null)
        	{
	            formWhereClause += ' and User_ID__c = :userId ';
        	}
        	
            if(isAdmin == true && roles != 'select' && isRoleEnabled)
                formWhereClause += ' and UserRoleId__c = :roles ';
            
            if(displayFlag == true && planDocument.Template__c != null)
                formWhereClause += ' and TemplateName__c like :templateName ';
                
            if(pdi.Effective_Start_Date__c == null)
                formWhereClause += ' and Effective_Start_Date__c != null';
                
            if(pdi.Effective_Start_Date__c != null)
                formWhereClause += ' and Effective_Start_Date__c >= :startDate ';
    
            if(pdi.Effective_End_Date__c != null)
                formWhereClause += ' and Effective_End_Date__c <= :endDate ';
    
            if(displayFlag == true && planDocument.Batch__c != null)
                formWhereClause += ' and Batch__c = :batchId ';
			
            contracts = Database.query('select Id, Name, FirstName__c, LastName__c, TemplateName__c, BatchName__c, Plan_Document_Name__c, Status__c, UserRoleName__c, Effective_Start_Date__c, Effective_End_Date__c, BatchID__c, UserID__c, ownerid from Sales_Contract__c where '+formWhereClause+' order by createddate desc limit 101');//order by CreatedDate Desc limit 101');
            Sales_Contract__Share[] scsList = new Sales_Contract__Share[]{}; 
            if(!isAdmin)
            {
                for(Sales_Contract__c scObj : contracts)
                {
                    Sales_Contract__Share scShare  = new Sales_Contract__Share();
                      
                    scShare.ParentId = scObj.Id;
                                      
                      // Set the ID of user or group being granted access.
                    scShare.UserOrGroupId = UserInfo.getUserId();
                      
                      // Set the access level.
                    scShare.AccessLevel = 'Edit';
                      
                    scShare.RowCause = Schema.Sales_Contract__Share.RowCause.Manual;
                      
                	if(scObj.ownerId != UserInfo.getUserId())
                		scsList.add(scShare);
            	}   
            }
            if(scsList.size() > 0)
                insert scsList;
                               
            if(contracts != null && contracts.size() > 100)
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, 'The search returned more than the maximum number of rows (100). Please refine your search criteria.'));
            if(contracts != null && contracts.size() == 0)
                contracts = null;
        }
        catch(Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
        }   
        return null;
    }
   
    static testMethod void testSearchPlanDocumentsForRmVF() 
    {
    	try
    	{
	        PageReference pg = Page.SearchPlanDocumentsForRm;
	        
	        Test.setCurrentPage(pg);
	
	        testVar = 'test';
	        User[] u = [select id from User where Firstname = 'Engineering'];
	        if(u.size() > 0)
	        {
	            system.runAs(u[0]){
	                SearchPlanDocumentsForRmVF spvf = new SearchPlanDocumentsForRmVF();
	                if(spvf != null)
	                {
	                    Template__c t = new Template__c(name = 'Test Search');
	                    insert t;
	                    Template__c[] tempT = [select name from Template__c where id = :t.id];
	                    system.assertEquals(t.name, tempT[0].name);
	                                 
	                    Batch__c b = new Batch__c(name = 'Test', Template__c = t.id, TemplateId__c = t.id);
	                    insert b;
	                    Batch__c[] batT = [select name, Template__c from Batch__c where id=:b.id];
	                    system.assertEquals(b.name, batT[0].name);
	                    system.assertEquals(b.Template__c, batT[0].Template__c);
	                    
	                    Sales_Contract__c sc = new Sales_Contract__c(BatchID__c = b.id, Template__c = t.id, TemplateName__c = t.name);
	                    insert sc;
	                    Sales_Contract__c[] scT = [select BatchID__c, Template__c from Sales_Contract__c where id=:sc.id];
	                    system.assertEquals(sc.BatchID__c, scT[0].BatchID__c);
	                    system.assertEquals(sc.Template__c, scT[0].Template__c);
	                    
	                    spvf.getDisplayColumns();
	                    spvf.setUserProfileName('System Administrator');
	                    system.assertEquals('System Administrator', spvf.getUserProfileName());
	                    spvf.getRolesList();
	                    spvf.setRoles(UserInfo.getUserRoleId());
	                    system.assertEquals(UserInfo.getUserRoleId(), spvf.getRoles());
	            
	                    spvf.setUserProfileName('Western Sales Team');
	                    system.assertEquals('Western Sales Team', spvf.getUserProfileName());
	                    spvf.getRolesList();
	                    spvf.setRoles(Userinfo.getUserRoleId());
	                    system.assertEquals(UserInfo.getUserRoleId(), spvf.getRoles());
	            
	                    List<SelectOption> options = new List<SelectOption>();
	                    options.add(new SelectOption('Viewed','Viewed'));
	                    options.add(new SelectOption('Published','Published'));
	                    options.add(new SelectOption('Accepted','Accepted'));
	                    options.add(new SelectOption('Inquiry Submitted','Inquiry Submitted'));
	                    options.add(new SelectOption('Inquiry Approved','Inquiry Approved'));
	                    options.add(new SelectOption('Inquiry Rejected','Inquiry Rejected'));
	                    system.assertEquals(options.get(options.size() - 1).getValue(), spvf.getStatusList().get(spvf.getStatusList().size() - 1).getValue());
	                    
	                    spvf.status = 'Published';
	                    system.assertEquals('Published', spvf.getStatus());
	                    
	                    spvf.setStatus('Published');
	                    
	                    spvf.setRoles(UserInfo.getUserRoleId());
	                    system.assertEquals(UserInfo.getUserRoleId(), spvf.getRoles());
	                    
	                    spvf.pdi = new Plan_Document_Inquiry__C();
	                    system.assertEquals(spvf.pdi, spvf.getStartDate());
	        
	                    system.assertEquals(null, spvf.doSearch());
	                    spvf.setUserProfileName('Western Sales Team');
	                    system.assertEquals(null, spvf.doSearch());
	                    spvf.setUserProfileName('System Administrator');
	                    system.assertEquals(null, spvf.doSearch());
	        
	                    if(spvf.contracts != null)
	                    spvf.setContracts(spvf.contracts);
	                    system.assertEquals(spvf.contracts, spvf.getContracts());
	                }  
	            }
	        }    
    	}
    	catch(exception e){}
    }
	

    static testMethod void testSearchPlanDocumentsForRmVF1() 
    {
    	try
    	{
	        User[] ul = [select id, Userrole.id, profileid from User where Firstname = 'Jack'];
	        User u = new User(Lastname = 'testBatchApexUser', Username = 'enduser@gmail.com', Email = 'enduser@gmail.com', Alias = 'enduser', CommunityNickname = 'euser', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'ISO-8859-1', ProfileId = ul[0].profileid, LanguageLocaleKey = 'en_US', ManagerId = ul[0].id, Userroleid = ul[0].Userrole.id);
			insert u;		 
	        PageReference pg = Page.SearchPlanDocumentsForRm;
	        
	        Test.setCurrentPage(pg);
	
			Test.startTest();
	        testVar = 'test';
	        if(ul.size() > 0)
	        {
	            system.runAs(u){
	            SearchPlanDocumentsForRmVF spvf = new SearchPlanDocumentsForRmVF();
	            spvf.isRoleEnabled = false;
	            
	            if(spvf != null)
	            {
	                
	                Sales_Contract__c sc = new Sales_Contract__c(User_ID__c = u.id);
	                insert sc;
	                Sales_Contract__c[] scT = [select id from Sales_Contract__c where id=:sc.id];
	                if(scT.size() > 0 )
	                	system.assertEquals(sc.id, scT[0].id);
	                
	                spvf.getDisplayColumns();
	                spvf.setUserProfileName('End User');
	                system.assertEquals('End User', spvf.getUserProfileName());
	                spvf.getRolesList();
	                spvf.setRoles(UserInfo.getUserRoleId());
	                system.assertEquals(UserInfo.getUserRoleId(), spvf.getRoles());
	        
	                spvf.setUserProfileName('Western Sales Team');
	                system.assertEquals('Western Sales Team', spvf.getUserProfileName());
	                spvf.getRolesList();
	                spvf.setRoles(u.Userrole.id);
	        
	                List<SelectOption> options = new List<SelectOption>();
	                options.add(new SelectOption('Viewed','Viewed'));
	                options.add(new SelectOption('Published','Published'));
	                options.add(new SelectOption('Accepted','Accepted'));
	                options.add(new SelectOption('Inquiry Submitted','Inquiry Submitted'));
	                options.add(new SelectOption('Inquiry Approved','Inquiry Approved'));
	                options.add(new SelectOption('Inquiry Rejected','Inquiry Rejected'));
	                system.assertEquals(options.get(options.size() - 1).getValue(), spvf.getStatusList().get(spvf.getStatusList().size() - 1).getValue());
	                
	                spvf.status = 'Published';
	                system.assertEquals('Published', spvf.getStatus());
	                
	                spvf.setStatus('Published');
	                
	                spvf.setRoles(u.Userrole.id);
	                
	                spvf.pdi = new Plan_Document_Inquiry__C();
	                system.assertEquals(spvf.pdi, spvf.getStartDate());
	    
	                system.assertEquals(null, spvf.doSearch());
	    
	                if(spvf.contracts != null)
	                spvf.setContracts(spvf.contracts);
	                system.assertEquals(spvf.contracts, spvf.getContracts());
	                }
	             }   
	        }    
	        Test.stoptest();
    	}
    	catch(exception e){}
    }
        
}//end of class